//package com.quark.listener;
//
//import cn.hutool.core.collection.CollUtil;
//import com.quark.common.security.handler.MyAuthenticationSuccessHandler;
//import com.quark.model.system.user.AdminUser;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.ApplicationListener;
//import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
//import org.springframework.security.core.Authentication;
//import org.springframework.stereotype.Component;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
///**
// * @author 张高昌
// * @date 2021/12/29 15:46
// * @description: 认证成功事件监听器, spring security防止恶意登录
// */
//@Component
//public class AuthenticationSuccessEventListener implements ApplicationListener<AuthenticationSuccessEvent> {
//
//    @Autowired(required = false)
//    private MyAuthenticationSuccessHandler successHandler;
//
//    @Override
//    public void onApplicationEvent(AuthenticationSuccessEvent event) {
//        Authentication authentication = (Authentication) event.getSource();
//        if (successHandler != null && isUserAuthentication(authentication)) {
//            ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
//                    .getRequestAttributes();
//            HttpServletRequest request = requestAttributes.getRequest();
//            HttpServletResponse response = requestAttributes.getResponse();
//
//            try {
//                successHandler.onAuthenticationSuccess(request, response, authentication);
//            } catch (IOException e) {
//                e.printStackTrace();
//            } catch (ServletException e) {
//                e.printStackTrace();
//            }
//        }
//    }
//
//    private boolean isUserAuthentication(Authentication authentication) {
//        return authentication.getPrincipal() instanceof AdminUser
//                || CollUtil.isNotEmpty(authentication.getAuthorities());
//    }
//}
